<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/3/13
 * Time: 14:08
 */
namespace  app\admin\controller;
use think\Db;
class  Banner extends Base{

    /*
       * 轮播图数据
       */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($type)&&$type != ""){
                $map['lb_type'] = $type;
            }
            if(isset($key)&&$key!="")
            {
                $map['lb_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('lunbo')->where($map)->count();
            $lists = Db::name('lunbo')
                ->where($map)
                ->field(['lb_name','lb_id','lb_type','lb_content','lb_image','lb_time, lb_status'])
                ->page($Nowpage,$limits)
                ->order('lb_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('banner/index');
    }

    /*
     * 添加轮播图
     */
    public function add(){
        if(request()->isPost ()){
            extract(input());
            $count = Db::table('up_lunbo')
                ->where('lb_type', $type)
                ->where('lb_status', 1)
                ->count();
            if($type == 1){
                if($count >= 3){
                    return json(['code'=>100,'msg'=>'首页轮播图数量不能超过3张']);
                }
            }else{
                if($count >= 1){
                    return json(['code'=>100,'msg'=>'我的背景图数量不能超过超过1张']);
                }
            }
                
            $time = date('Y-m-d H:i:s');
            $param = [
                'lb_name'=>$lb_name,
                'lb_image'=>$img,
                'lb_type'=>$type,
                'lb_time'=>$time,
                'lb_where' => $lb_where
            ];
            
            if($type == 1){
                if($lb_where == 1){
                    $param['lb_content'] = $house_id;
                }elseif($lb_where == 2){
                    $param['lb_content'] = '';
                }elseif($lb_where == 3){
                    $param['lb_content'] = $company_id;
                }else{
                    $param['lb_content'] = $lb_wailian;
                }
            }
            $res = Db::name('lunbo')->insert ($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        //房源
        $house = Db::table('up_house')
            ->where('h_status', 1)
            ->field('h_id, h_title')
            ->select();
        //装修公司
        $company = Db::table('up_company')
            ->field('id, name')
            ->select();
        $this->assign([
            'house' => $house,
            'company' => $company
        ]);
        return $this->fetch('banner/add');
    }

    /*
     * 编辑轮播图
     */
    public function edit(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'lb_type'=>$type,
                'lb_content'=>$content,
                'lb_image'=>$img,
                'lb_time'=>$time,
                'lb_name'=>$lb_name,
            ];
            $res = Db::name('lunbo')->where('lb_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        //轮播图
        $info = Db::name('lunbo')
            ->where(['lb_id'=>input('id')])
            ->find();
        //房源
        $house = Db::table('up_house')
            ->where('h_status', 1)
            ->field('h_id, h_title')
            ->select();
        //装修公司
        $company = Db::table('up_company')
            ->field('id, name')
            ->select();
        $this->assign([
            'house' => $house,
            'company' => $company,
            'info' => $info
        ]);
        return $this->fetch('banner/edit');
    }

    /*
     * 删除轮播图
     */
    public function del(){
        $count = Db::table('up_lunbo')->where('lb_type', 1)->count();
        if($count <= 1){
            return json(['code'=>100,'msg'=>'只有一张轮播图，不可删除']);
        }
        $res = Db::name('lunbo')->where('lb_id',input('id'))->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /****
     * 轮播图的禁用，启用
     */
    public function status()
    {
        $id = input('param.id');
        $lb_status = Db::table('up_lunbo')->where('lb_id',$id)->value('lb_status');
        if($lb_status==1){
            $status = 2;
        }elseif ($lb_status==2){
            $status= 1;
        }
        $data['lb_status'] = $status;
        //-------------------------------
        if($status == 1){
            $type = Db::table('up_lunbo')->where('lb_id',$id)->value('lb_type');
            $count = Db::table('up_lunbo')
                ->where('lb_type', $type)
                ->where('lb_status', 1)
                ->count();
            if($type == 1 && $count >= 3){
                return json(['code'=>100,'msg'=>'首页轮播图数量不能超过3张']);
            }
            if($type == 2 && $count >= 1){
                return json(['code'=>100,'msg'=>'我的背景图数量不能超过超过1张']);
            }
        }
        //-------------------------------
        $res = Db::table('up_lunbo')->where('lb_id',$id)->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
}